package com.health.mapp.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.log4j.Logger;

public class PropertiesUtil {

	public static final String upload_path_properties_windows = "resources\\properties\\resources.properties";
	public static final String upload_path_properties_unix = "resources/properties/resources.properties";
	
	 private static final String BUNDLE_NAME = "resources";
		
		
		
	 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
	
	private static final String JDBC_PROPERTIES = "WEB-INF/jdbc.properties";

	public static final String Test_Key = "Test";
	 

	private static final Logger logger = Logger.getLogger(PropertiesUtil.class
			.getName());

	/**
	 * Map   [key,value]
	 * 
	 * @return
	 */
  	public static Map<String, String> createConfig_Upload_Map() {
 		return PropertiesUtil.parseProperties(RESOURCE_BUNDLE);
  	}
//
//	  
//	 
// 	public static boolean isTest() {
// 		final boolean is = PropertiesUtil.isOnOff(producePath(upload_path_properties), Test_Key);
// 		logger.info("Test : " + is);
// 		return is;
// 	}
// 
 	 
 

	/**
	 * 
	 * @param map
	 * @return
	 */
	public static Map<String, String> reverseMap(Map<String, String> map) {
		// {AK=SN, BK=XN, CK=BN, HK=AN, SK=HN}
		if (map == null)
			return null;
		Map<String, String> tm = new TreeMap<String, String>();
		Set<String> set = map.keySet();
		for (String key : set) {
			tm.put(map.get(key), key);
		}
		// {AN=HK, BN=CK, HN=SK, SN=AK, XN=BK}
		return tm;
	}

	/**
	 * ˵���Ƿ�����
	 * 
	 * @param path
	 * @return
	 */
	private static boolean isOnOff(String path, String onOffKey) {
		if (!new File(path).exists())
			return false;
		Properties ps = PropertiesUtil.createProperties(path);
		Enumeration<?> e = ps.propertyNames();
		if (e.hasMoreElements()) {
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				if (key.trim().equals(onOffKey)) {
					String use = ps.getProperty(key);
					if (use == null || use.trim().equals(""))
						return false;
					if (use.trim().equals("true"))
						return true;
				}
			}
		}
		return false;
	}

	public static Properties createProperties(String path) {
		if ((path == null) || path.equals("")) {
			System.out.println("");
			return null;
		}

		Properties properties = new Properties();
		File file = new File(path);
		FileInputStream fis;

		try {
			fis = new FileInputStream(file);
			properties.load(fis);
			fis.close();
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return properties;
	}

	public static SortedMap<String, String> parseProperties(ResourceBundle RESOURCE_BUNDLE) {
		SortedMap<String, String> map = new TreeMap<String, String>();
		 
		 
			//空，则返回空
			if (RESOURCE_BUNDLE == null)
			{
				logger.info(" properties  is null");
				return new TreeMap<String, String>();
			}
			
			Enumeration<?> e = RESOURCE_BUNDLE.getKeys();
			if (e.hasMoreElements()) {
				while (e.hasMoreElements()) {
					final String key = (String) e.nextElement();
					if (key != null && !key.trim().equals("")) {
						String value = RESOURCE_BUNDLE.getString(key);
						
					 //	logger.info("key is : " + value);
						map.put(key.trim(), value);
					}
				}
			}
			return map;
	}

	public static String producePath(String path,String file) {
			//final ClassLoader loader = PropertiesUtil.class.getClassLoader();
			//String path = loader.getResource("../../").getPath();
			
			logger.info("path:"+path);
			final String target = "%20";
			if (path.contains(target)) {
				logger.info(path);
				path = path.replace(target, " ");
			}
			String sourceFileName = null;
			path = path.trim();
			if (path.endsWith("/") || path.endsWith("\\")) {
				
				sourceFileName = path + file;
			} else {
				
			
				sourceFileName = path + "/" + file;
			}
			logger.info("sourceFileName:"+sourceFileName);
			return sourceFileName;
		}

	/**
	 * 
	 * @param regex
	 * @return Pattern
	 */
	public static Pattern compile(String regex) {
		try {
			return Pattern.compile(regex);
		} catch (PatternSyntaxException pse) {
			
			logger.error(pse.getCause());
		}
		return null;
	}
	
	
	private PropertiesUtil() {
	}

	public static String getString(String key) {
		try {
			
		 
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			
			
			e.printStackTrace();
			return '!' + key + '!';
		}
	}

	public static int getInteger(String key, int defaultInt) {
		try {
			return Integer.valueOf(RESOURCE_BUNDLE.getString(key));
		} catch (MissingResourceException e) {
			e.printStackTrace();
			return defaultInt;
		}
	}
	
	public static long getLong(String key, int defaultInt) {
		try {
			return Long.valueOf(RESOURCE_BUNDLE.getString(key));
		} catch (MissingResourceException e) {
			e.printStackTrace();
			return defaultInt;
		}
	}
	
}
